package user

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/redPacketClient/red_packet_rpc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddRedPacketAmountLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员管理 - 会员管理 - 给代理用户充值红包金额
func NewAddRedPacketAmountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddRedPacketAmountLogic {
	return &AddRedPacketAmountLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddRedPacketAmountLogic) AddRedPacketAmount(req *types.RedPacketAmountRechargeRequest) (resp *types.RedPacketAmountRechargeResponse, err error) {
	adminUserId := headInfo.GetTokenUid(l.ctx)
	adminUserInfo, err := l.svcCtx.UserServiceRPC.AdminUserInfo(l.ctx, &userrpc.AdminUserInfoRequest{
		Id: adminUserId,
	})
	if err != nil {
		return nil, err
	}
	adminAccount := adminUserInfo.AdminUser.Account

	_, err = l.svcCtx.RedPacketRpc.AgentAmountAdd(l.ctx, &red_packet_rpc.AgentAmountAddRequest{
		AgentId:      req.UserId,
		Amount:       req.Amount,
		OperatorId:   adminUserId,
		OperatorName: adminAccount,
	})
	if err != nil {
		return nil, err
	}
	resp = &types.RedPacketAmountRechargeResponse{}
	return
}
